package com.yoso.base.interfaces.controller;

import com.yoso.base.infrastructure.constants.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author shizejin
 * @date 2021/11/2
 * @description: 响应
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseResponse<T> {
    private String code;
    private T data;
    private String message;
    private boolean result = true;

    private static final String SUCCESS_MSG = "操作成功";
    private static final String ERROR_MSG = "操作失败";

    public BaseResponse(String code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public static BaseResponse<Void> error(String msg) {
        return new BaseResponse<>(ResponseCode.ERROR, null, msg, false);
    }

    public static BaseResponse<Void> error() {
        return new BaseResponse<>(ResponseCode.ERROR, null, ERROR_MSG, false);
    }

    public static BaseResponse<Void> success(String msg) {
        return new BaseResponse<>(ResponseCode.SUCCESS, null, msg);
    }

    public static BaseResponse<Void> success() {
        return new BaseResponse<>(ResponseCode.SUCCESS, null, SUCCESS_MSG);
    }

    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(ResponseCode.SUCCESS, data, BaseResponse.SUCCESS_MSG);
    }

    public static <T> BaseResponse<T> success(String msg, T data) {
        return new BaseResponse<>(ResponseCode.SUCCESS, data, msg);
    }

    public static BaseResponse<Void> result(int result) {
        if (result == 1) {
            return new BaseResponse<>(ResponseCode.SUCCESS, null, BaseResponse.SUCCESS_MSG);
        }
        return new BaseResponse<>(ResponseCode.ERROR, null, BaseResponse.SUCCESS_MSG, false);
    }

    public boolean isSuccess() {
        return this.result;
    }
}
